<?php
/**
 * Author:Albert Zhan(http://www.5lazy.cn)
 * Date: 2017/4/1 0001
 * Time: 15:18
 */
namespace Weixin\Controller;
class AdminController extends \Common\Controller\BaseController{

    //初始化时判断管理员身份
    public function _initialize(){
        if(empty($_SESSION['admin'])){
            $userid='x1x1x1';
            if($userid!='x1x1x1'){
                $this->rtnResult(99,'权限不足,非法操作!');
                exit;
            }else{
                $_SESSION['admin']=$userid;
            }
        }
    }

    /**
     * 车牌号列表
     */
    public function CarNoList(){
        $this->display();
    }

    /**
     * 获取车牌号列表
     */
    public function GetCarNoList(){
        $where['_string']='o.fk_user_id=u.pk_id';
        $where['o.status']=0;
        $field="o.pk_id,o.query_car_no,o.query_datetime,o.stage,o.create_time,u.name";
        $rows=M()->table(array('order'=>'o','user'=>'u'))->page(I('post.page',1),10)->where($where)->field($field)->order('pk_id DESC')->select();
        $this->data['rows']=$rows;
        $this->rtnResult(0);
    }

    /**
     * 车牌号详情
     */
    public function CarNoInfo(){
        $pk_id=I('get.pk_id');
        !$pk_id && exit('参数不全');
        $where['_string']='o.fk_user_id=u.pk_id';
        $where['o.pk_id']=$pk_id;
        $where['o.stage']=1;
        $where['o.status']=0;
        $field="o.pk_id,o.query_car_no,o.query_datetime,o.create_time,u.name";
        $rows=M()->table(array('order'=>'o','user'=>'u'))->where($where)->field($field)->find();
        if(empty($rows)){
            exit('非法操作');
        }else{
            $this->assign('rows',$rows);
            $this->display();
        }
    }

    /**
     * 更新车牌号详情
     */
    public function UpdateCarNoInfo(){
        $pk_id=I('post.pk_id');
        !$pk_id && $this->rtnResult(1,'非法操作');
        if(!empty($_FILES)){
            $path='original_img/'.date('Y-m').'/';
            $return=UploadAll(null,$path);
            if(!is_array($return)){
                $this->rtnResult(1,$return);
            }else{
                //写入原始带水印图片到original_img表中
                $original_model=M('original_img');
                $pic_path=array();
                foreach($return as $k=>$v){
                    $data=array(
                        'order_id'=>$pk_id,
                        'path'=>'/Uploads/'.$v['savepath'].$v['savename'],
                        'create_time'=>date('Y-m-d H:i:s'),
                    );
                    $id=$original_model->add($data);
                    $pic_path[$k]=array(
                        'ori_img_id'=>$id,
                        'path'=>'Uploads/'.$v['savepath'].$v['savename'],
                    );
                }
                //处理水印
                $save_path=$this->HandleWatermark($pic_path);
                //写入处理过水印的图片到finish_img表中
                $finish_model=M('finish_img');
                foreach($save_path as $k=>$v){
                    $data=array(
                        'order_id'=>$pk_id,
                        'ori_img_id'=>$v['ori_img_id'],
                        'path'=>$v['path'],
                        'create_time'=>date('Y-m-d H:i:s'),
                    );
                    $finish_model->add($data);
                }
            }
        }
        if(M('order')->where("pk_id=%d AND status=%d",array($pk_id,0))->save(array('stage'=>2))){
            $this->rtnResult(0,'发送成功');
        }else{
            $this->rtnResult(1,'发送失败');
        }

    }

    /**
     * 处理带水印的图片
     * @param array $pic_path 处理图片的数组路径
     * @return array|bool
     */
    private function HandleWatermark(array $pic_path){
        $objimg=new \Think\Image();
        $save_path=array();
        foreach($pic_path as $k=>$v){
            //裁剪车牌号码生成临时文件到根目录
            $objimg->open(ROOT_PATH.$v['path'])->crop(100,620,120,30)->save(ROOT_PATH.'save1.jpg');
            //裁剪经过时间、经过区域、名称、方向
            $objimg->open(ROOT_PATH.$v['path'])->crop(635,620,590,30)->save(ROOT_PATH.'save2.jpg');
            if(!is_dir(ROOT_PATH.'Uploads/finish_img/')){
                mkdir(ROOT_PATH.'Uploads/finish_img/');
            }
            if(!is_dir(ROOT_PATH.'Uploads/finish_img/'.date('Y-m').'/')){
                mkdir(ROOT_PATH.'Uploads/finish_img/'.date('Y-m').'/');
            }
            $path='Uploads/finish_img/'.date('Y-m').'/'.uniqid().'.png';
            //合成图片
            $objimg->CompositeImage(array(ROOT_PATH.'save1.jpg',ROOT_PATH.'save2.jpg'),ROOT_PATH.$path);
            //把信息组成数组，返回给调用端
            $save_path[$k]['ori_img_id']=$v['ori_img_id'];
            $save_path[$k]['path']='/'.$path;
            //删除裁剪出的临时文件
            unlink(ROOT_PATH.'save1.jpg');
            unlink(ROOT_PATH.'save2.jpg');
        }
        return $save_path;
    }

}